<template>
	<view>
		<view style="width:100%;height:150rpx;"></view>
		<view class="shopBut">
			<view class="for">
				<view><text>原     价</text><text>¥</text><text>{{addNums==0?totalMoney:'--'}}</text></view>
				<view><text>平台补贴</text><text>-¥</text><text>{{addNums==0?yhPrice:'--'}}</text></view>
				<view style="color: #F54B39;">
					<text style="font-size: 28rpx;">{{text}}</text>
					<text>¥</text>
					<text style="font-size: 32rpx;">{{addNums==0?money:'--'}}</text>
				</view>
			</view>
			<view  class="flex-end">
				<!-- #ifdef H5 -->
				<text class="shares" v-if="share" @click="openShare()">{{share}}</text>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<button class="shares" open-type="share" v-if="share">分享</button>
				<!-- #endif -->
			    <text @click="submit()" :class="empty?'pays':'pay'">{{sures?'微信支付':'点击购买'}}</text>
			</view>
		</view>
		<!-- 新建团惠包弹框 -->
		<uni-popup ref="PobulicePopup" type="center" :is-mask-click="false">
		  <pobulice :title="'温馨提示'" :text="title" @close="closeBankPad" @submit="BuyAgain" :type="'center'"></pobulice>
		</uni-popup>
		<!-- 分享面板 -->
		<uni-popup ref="sharePopup" type="center">
		  <view class="shareImg" @click="closeShare">
			  <image :src="share1" mode=""></image>
			  <image :src="share2" mode=""></image>
		  </view>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
import { navTo } from '@/utils/navigator';
import { ref } from 'vue';
import pobulice from '@c/pobulic/public.vue';
import { onLoad } from '@dcloudio/uni-app';
import { errorToast, successToast, showLoading } from '@/utils/prompt';
import { isH5 } from '@/utils/platform';
import { toPublish } from '@mqtt';
import { numsID } from '@/gql/group';
import { share1, share2, poster3 } from '@/utils/Static';
import { getDriverID } from '@/stores/driverID';
const driver_id = getDriverID();
const PobulicePopup = ref(null);
const sharePopup = ref(null);
const repeat = ref(true); //放重复点击
const title = '该团惠包商品中有过期订单 您是否还要重新购买';
const props = defineProps({
  share: { type: String },
  money: { type: Number },
  totalMoney: { type: Number },
  yhPrice: { type: Number },
  text: { type: String },
  sures: { type: Boolean },
  empty: { type: Boolean },
  addNums: { type: Number },
  groupID: { type: String },
  state: { type: String },
  title: { type: String },
  des: { type: String },
  bao_id: { type: String },
  teamID: { type: String },
  fxID: { type: String },
});
const emit = defineEmits(['submit', 'sharePop']);
function submit() {
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  switch (props.state) {
    case 'edit':
      if (props.empty) {
        const payload = {
          user_id: driver_id,
          title: props.title,
          des: props.des,
        };
        toPublish('store/addTeamOrder', payload, (obj: any) => {
          const { code, msg, data } = obj;
          if (code === 1) {
            repeat.value = true;
            successToast(msg);
            navTo('groupBenefits/placeOrder?id=' + data.order_id);
          } else if (code === 0) {
            emit('submit');
            repeat.value = true;
            errorToast(msg);
          } else {
            repeat.value = true;
            errorToast(msg);
          }
        });
      }
      break;
    case 'sureOrder':
      const payload = {
        order_id: Number(props.bao_id),
        method: isH5 ? 'mp' : 'mini',
      };
      toPublish('store/payWechatByOrderBao', payload, (obj: any) => {
        const { code, msg, data } = obj;
        if (code === 1) {
          weach(data);
        } else {
          repeat.value = true;
          errorToast(msg);
        }
      });
      break;
    case 'details':
      const payloads = {
        user_id: driver_id,
        bao_id: Number(props.bao_id),
        fx_user_id: Number(props.fxID),
        team_id: Number(props.teamID),
      };
      toPublish('store/addTeamBaoOrder', payloads, (obj: any) => {
        const { code, msg, data } = obj;
        if (code === 1) {
          repeat.value = true;
          navTo('groupBenefits/placeOrder?id=' + data.order_id);
        } else if (code === 0) {
          repeat.value = true;
          openBankPad();
        } else {
          repeat.value = true;
          errorToast(msg);
        }
      });
      break;
  }
}

function weach(num) {
  repeat.value = true;
  WeixinJSBridge.invoke('getBrandWCPayRequest', num, function(res) {
    switch (res.err_msg) {
      case 'get_brand_wcpay_request:ok':
        orderDetail('1');
        break;
      case 'get_brand_wcpay_request:cancel':
        errorToast('取消支付');
        orderDetail('2');
        break;
      case 'get_brand_wcpay_request:fail':
        errorToast('支付失败');
        break;
    }
  });
}
/**
 * @param {String} num 支付状态1 支付成功 2支付失败 请稍后重试
 */
function orderDetail(num) {
  const payload = {
    query: numsID,
    variables: {
      order_id: Number(props.bao_id),
    },
  };
  toPublish('ql/store/getTeamOrderInfo', payload, (obj: any) => {
    const { team_order_info } = obj.data;
    if (num == '1') {
      uni.redirectTo({
        url: '/pages/groupBenefits/groupSuccess?baoId=' + team_order_info.bao_id + '&id=' + team_order_info.id,
      });
    } else {
      uni.redirectTo({
        url: '/pages/groupBenefits/groupDetails?id=' + team_order_info.bao_id + '&type=create',
      });
    }
  });
}

/**
 * 清空当前团惠包购物车
 */
function BuyAgain() {
	 showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id: driver_id,
  };
  toPublish('store/clearTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    closeBankPad();
    if (code === 1) {
			repeat.value = true
      addGroup();
    } else {
			repeat.value = true
      errorToast(msg);
    }
  });
}
/**
 * 创建当前团惠包订单
 */
function addGroup() {
	 showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id: driver_id,
    bao_id: Number(props.bao_id),
  };
  toPublish('store/addTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    if (code === 1) {
			repeat.value = true
      successToast(msg);
      navTo('groupBenefits/groupEditor');
    } else {
			repeat.value = true
      errorToast(msg);
    }
  });
}
/**
 * 支付失败打开弹框
 */
function openBankPad() {
  PobulicePopup.value.open();
}
/**
 * 关闭支付报错弹框
 */
function closeBankPad() {
  PobulicePopup.value.close();
}
/**
 * H5分享提示页面
 */
function openShare() {
  sharePopup.value.open();
}
function closeShare() {
  sharePopup.value.close();
}
const teamID = ref('');
onLoad((pearm) => {
  teamID.value = pearm.team;
});
</script>

<style scoped lang="less">
.shopBut {
  width: 100%;
  position: fixed;
  bottom: 0px;
  left: 0px;
  z-index: 9;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 150rpx;
  padding: 0px 20rpx 0rpx 40rpx;
  box-sizing: border-box;
  background-color: white;
  .for {
    view {
      color: #666666;
      text:nth-of-type(1) {
        display: inline-block;
        font-size: 24rpx;
        font-weight: 400;
        width: 110rpx;
        text-align: justify;
        text-justify: distribute-all-lines;
        text-align-last: justify;
      }
      text:nth-of-type(2) {
        font-size: 20rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        margin: 0px 5rpx 0px 35rpx;
      }
      text:nth-of-type(3) {
        font-size: 28rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
      }
    }
  }
}
.shares {
  width: 156rpx;
  height: 80rpx;
  background: #ffffff;
  border-radius: 5px;
  border: 1px solid #1b91ff;
  text-align: center;
  line-height: 80rpx;
  font-size: 36rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #1b91ff;
  margin-right: 20rpx;
}
.pays {
  padding: 20rpx 30rpx;
  box-sizing: border-box;
  border-radius: 5px;
  min-width: 150rpx;
  text-align: center;
  background: #2c99ff;
  font-size: 36rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #fff;
}
.pay {
  padding: 20rpx 30rpx;
  box-sizing: border-box;
  border-radius: 5px;
  min-width: 150rpx;
  text-align: center;
  background: #f2f2f2;
  font-size: 36rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #666666;
}
.shareImg {
  width: 100%;
  height: 80vh;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  image:nth-of-type(1) {
    width: 201rpx;
    height: 214rpx;
  }
  image:nth-of-type(2) {
    width: 448rpx;
    height: 208rpx;
  }
}
</style>